﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;

using GameCore;
using GameLogic;
namespace GameEditor
{
    /// <summary>
    /// 资源配置文件工具
    /// </summary>
    public class ResPathConfigTool
    {
        private const string ResDirName = "ManagedResources/";

        private static string GetResLocalPath(string fullPath)
        {
            var strindex = fullPath.IndexOf(ResDirName);
            if (strindex == -1) return fullPath;
            var localDir = fullPath.Substring(strindex + ResDirName.Length) + "/";
            return localDir;
        }

        public static void CreateResPathConfig() {

            List<string> dirPaths=new List<string>();
            List<string> abNames=new List<string>();
            Dictionary<string, ResPathInfo> resPaths = new Dictionary<string, ResPathInfo>();
            ListDirectories(EditorConst.ManagedResourcesPath, dirPaths, abNames, resPaths);
            //初始化配置类
            string assetPath = AppConst.ConfigsPath + "ResourcePathConfig.asset";
            string fullPath = EditorUtil.GetFullPath(assetPath);
            ResourcePathConfig configClass = null;
            if (!File.Exists(fullPath))
            {
                configClass = ScriptableObject.CreateInstance<ResourcePathConfig>();
                configClass.Init(dirPaths, abNames, resPaths);
                AssetDatabase.CreateAsset(configClass, assetPath);
            }
            else
            {
                configClass = AssetDatabase.LoadAssetAtPath<ResourcePathConfig>(assetPath);
                configClass.Init(dirPaths, abNames, resPaths);
            }
            EditorUtility.SetDirty(configClass);
            EditorUtility.ClearProgressBar();
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

        }

        public static void ListDirectories(string path,List<string> dirPaths, List<string> abNames, Dictionary<string, ResPathInfo> resPaths, AssetImporter ai =null) {
            string assetPath = EditorUtil.GetAssetsPath(path);
            List<string> files = EditorUtil.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
            if (files.Count > 0 && dirPaths.IndexOf(assetPath) == -1)
            {
                dirPaths.Add(assetPath);
            }
            if (ai==null) ai = AssetImporter.GetAtPath(assetPath);
            if (IsMarked(ai))
            {
                if (abNames.IndexOf(ai.assetBundleName) == -1) abNames.Add(ai.assetBundleName);
            }
            else {
                ai = null;
            }
            for (int i = 0; i < files.Count; i++)
            {
                ListFiles(files[i], dirPaths, abNames, resPaths, ai);
            }
            List<string> directories = EditorUtil.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < directories.Count; i++) {
                ListDirectories(directories[i],dirPaths,abNames,resPaths,ai);
            }
        }

        public static void ListFiles(string path, List<string> dirPaths, List<string> abNames, Dictionary<string, ResPathInfo> resPaths, AssetImporter ai = null) {
            if (path.Contains(".meta")) return;
            string assetPath = EditorUtil.GetAssetsPath(path);
            if (ai == null) ai = AssetImporter.GetAtPath(assetPath);
            if (ai == null) return;
            string fileName = Path.GetFileNameWithoutExtension(path);
            string extension = Path.GetExtension(path);
            if (IsMarked(ai)) {
                if (abNames.IndexOf(ai.assetBundleName) == -1) abNames.Add(ai.assetBundleName);
            }
            if (resPaths.ContainsKey(fileName))
            {
                Debug.LogErrorFormat("资源名重复:{0}",fileName);
            }
            else {
                resPaths.Add(fileName, new ResPathInfo(fileName, extension, dirPaths.Count-1, abNames.Count-1));
            }
        }

        public static bool IsMarked(AssetImporter ai) {
            return ai != null && !string.IsNullOrEmpty(ai.assetBundleName) && !string.IsNullOrEmpty(ai.assetBundleVariant);
        }
    }
}

